package com.cosina.game.crystallight;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.cosina.game.crystallight.engine.BattleEngine;
import com.cosina.game.crystallight.model.profile.ProfileManager;
import com.cosina.game.crystallight.ui.ResourceManager;
import com.cosina.game.crystallight.ui.activity.ActivityStatus;
import com.cosina.game.crystallight.ui.activity.ActivityStatusView;
import com.cosina.game.crystallight.ui.battle.BattleResource;
import com.cosina.game.crystallight.ui.battle.BattleView;
import com.cosina.game.crystallight.ui.mapselect.UpAndDownLayout;
import com.cosina.game.crystallight.ui.menu.CreditsView;
import com.cosina.game.crystallight.ui.menu.LoadingView;
import com.cosina.game.crystallight.ui.menu.ProfileMenuView;
import com.cosina.game.crystallight.ui.menu.MainMenuView;
import com.cosina.game.crystallight.util.ActivityUtil;

public class CrystalLightActivity extends Activity {
	
	private ActivityStatus status;
	private FrameLayout rootLayout;
	private View currentView;
	public ActivityStatusView statusView;
	public static CrystalLightActivity self;
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		self = this;
		
		MediaPlayerAdapter.init(this);
		status = new ActivityStatus(this);
		ResourceManager.init(this);

		ActivityUtil.noNotificationBar(this);
		ActivityUtil.noTitleBar(this);

		
		rootLayout = new FrameLayout(this);
		statusView = new ActivityStatusView(this);
		this.setContentView(rootLayout);
		doShowLoadingMain();
//		 doPlayTollGate(4);
	}

	public ActivityStatus getStatus(){
		return status;
	}
	
	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		return status.menuPrepareProxy(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		return status.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode == KeyEvent.KEYCODE_VOLUME_UP){
			if(status.isRunning() && status.isSoundOn()){
				MediaPlayerAdapter.volumeUp();
				status.tryVolumeUp();
			}
			return true;
		}else if(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN){
			if(status.isRunning() && status.isSoundOn()){
				MediaPlayerAdapter.volumeDown();
				status.tryVolumeDown();
			}
			return true;
		}
		if(currentView.onKeyDown(keyCode, event)){
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public boolean onKeyUp(int keyCode, KeyEvent event) {
		return currentView.onKeyUp(keyCode, event);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		if (null != currentView && status.isRunning()) {
			return currentView.dispatchTouchEvent(ev);
		}
		return true;
	}
	
	public void doShowLoadingMain() {
		if(null != currentView)
			rootLayout.removeView(currentView);	
		currentView = new LoadingView(this);
		rootLayout.addView(currentView, 0);
	}

	
	private void doshowView(View newView){
		rootLayout.removeAllViews();
		currentView = newView;
		rootLayout.addView(currentView);
		rootLayout.addView(statusView);
	}
	
	public void doShowMenu() {
		MediaPlayerAdapter.menuArrival();
		doshowView(new MainMenuView(this));
	}

	public void doShowProfileMenu() {
		doshowView(new ProfileMenuView(this));
	}

	public void doShowCreditMenu() {
		doshowView(new CreditsView(this));
	}

	public void doShowMap() {
		if(null == ProfileManager.getCurrentProfile()){
			doShowProfileMenu();
		}else{
			doshowView(new UpAndDownLayout(this));
		}
	}

	public void doPlayTollGate(int tollGateId) {
		BattleResource.init();
		BattleEngine.start(tollGateId);
		doshowView(new BattleView(this));
	}
	

}